package com.tjetc.service.impl;

import com.tjetc.dao.AdoptApplicationMapper;
import com.tjetc.dao.AnimalMapper;
import com.tjetc.dao.HistoryMapper;
import com.tjetc.domain.AdoptionApplication;
import com.tjetc.domain.Animal;
import com.tjetc.service.AdoptApplicationService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

public class AdoptApplicationServiceImpl implements AdoptApplicationService {
    private AnimalMapper animalMapper= Db.getMapper(AnimalMapper.class);
    private HistoryMapper historyMapper=Db.getMapper(HistoryMapper.class);
    private AdoptApplicationMapper adoptApplicationMapper=Db.getMapper(AdoptApplicationMapper.class);
    @Override
    public Result list() {
        return new Result(200,null,animalMapper.list(0));
    }

    @Override
    public Result getById(int id) {
        Animal animal=adoptApplicationMapper.getById(id);
        if (animal!=null){
            return new Result(200,null,animal);
        }else {
            return new Result(500,null,"没有该宠物");

        }
    }

    @Override
    public Result addApplication(AdoptionApplication application) {
        Animal animal=adoptApplicationMapper.getById(application.getAnimalId());
        if (animal!=null) {
            int rows = adoptApplicationMapper.addApplication(application);
            if (rows==1){
                animalMapper.update(new Animal().setAdopted(2).setId(application.getAnimalId()));
                return new Result(200,"申请成功",null);
            }else {
                return new Result(500,"申请失败",null);
            }
        }else {
            return new Result(500,"没有该宠物了",null);
        }
    }

    @Override
    public Result listApplication(int id) {
        return new Result(200,null,adoptApplicationMapper.listApplication(id));
    }

    @Override
    public Result listHistory(int id) {
        return new Result(200,null,historyMapper.listHistory(id));
    }

}
